#include "tempdrawui.h"
#include "ui_tempdrawui.h"

#include <numeric>
#include <QPainter>
#include <QPainterPath>

TempDrawUI::TempDrawUI(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TempDrawUI)
{
    ui->setupUi(this);
}

TempDrawUI::~TempDrawUI()
{
    delete ui;
}

void TempDrawUI::setTemps(const QVector<double> &temps) {
    // 数据验证
    if (temps.size() != 7) {
        qWarning() << "需要7个温度数据，当前收到:" << temps.size();
        return;
    }

    m_temps = temps;            // 保存温度数据
    getAverage();               // 计算平均值
    update();                   // 触发重绘
}

void TempDrawUI::getAverage() {
    m_average = std::accumulate(m_temps.begin(),
                                m_temps.end(), 0.0) / m_temps.size();

}

void TempDrawUI::paintEvent(QPaintEvent *) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 创建圆角矩形路径
    QPainterPath backgroundPath;
    const int radius = 10;  // 圆角半径
    backgroundPath.addRoundedRect(rect(), radius, radius);

    // 绘制圆角背景
    painter.fillPath(backgroundPath, QColor(250, 250, 245));

    // 设置剪切区域（确保后续绘制不超出圆角范围）
    painter.setClipPath(backgroundPath);
    // 计算动态缩放比例
    double maxDiff = 0.0;
    for (const double temp : qAsConst(m_temps)) {
        const double diff = qAbs(temp - m_average);
        maxDiff = qMax(maxDiff, diff);
    }

    // 设置安全边距（考虑点和标签高度）
    const int margin = 30;
    const int availableHeight = (height()/2) - margin;
    const double scaleFactor = (maxDiff > 0.0 && availableHeight > 0) ?  availableHeight / maxDiff : 15.0;

    // 绘制辅助线
    painter.setPen(QPen(QColor(180, 180, 180), 1, Qt::DashLine));
    painter.drawLine(0, height()/2, width(), height()/2); // 平均温度线

    // 计算坐标点
    const int segmentWidth = width() / 7;
    QVector<QPointF> points;

    for (int i = 0; i < 7; ++i) {
        const double temp = m_temps[i];
        const int x = (i * segmentWidth) + (segmentWidth / 2);
        const int yOffset = static_cast<int>((temp - m_average) * scaleFactor);
        const int y = qBound(margin,                           // 最小Y坐标
                             height()/2 - yOffset,
                             height() - margin);               // 最大Y坐标
        points << QPointF(x, y);
    }

    // 绘制温度曲线
    if (!points.isEmpty()) {
        QPainterPath path;
        path.moveTo(points.first());

        // 创建平滑曲线
        for (int i = 1; i < points.size(); ++i) {
            path.lineTo(points[i]);
        }

        painter.setPen(QPen(QColor(72, 145, 217), 3));
        painter.drawPath(path);
    }

    // 绘制数据点及标签
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(0, 0, 0));

    for (const auto& point : points) {
        // 绘制数据点
        painter.drawEllipse(point, 3, 3);

        // 智能调整标签位置
        painter.setPen(QColor(80, 80, 80));
        const QString text = QString::number(m_temps[points.indexOf(point)], 'f', 1) + "°";
        QRect textRect;

        if (point.y() < height()/2) { // 上半部分显示在下方
            textRect = QRect(point.x() - 25, point.y() + 5, 50, 20);
        } else { // 下半部分显示在上方
            textRect = QRect(point.x() - 25, point.y() - 20, 50, 20);
        }

        // 确保标签在窗口范围内
        textRect.moveTop(qBound(0, textRect.top(), height() - textRect.height()));
        painter.drawText(textRect, Qt::AlignCenter, text);
    }
}
